#include "stream/Appender.h"
#include <iostream>
#include "stream/Topic.h"

namespace stream {

class MyAppender : public Appender {
public:
	MyAppender(std::shared_ptr<Topic> topic)
	:_topic(topic)
	{
	}
	~MyAppender() override {
        this->close();
    }

	int append(size_t index, const std::string & str) override {
		return _topic->append(std::to_string(index), str);
	}
	size_t size() const override{
		return _topic->size();
	}
protected:
	bool _open() override {
		return _topic->open();
	}
	void _close() override {
		std::cout << _topic.use_count() << std::endl;

		if(_topic.use_count() <= 1){
			return _topic->close();
		}
	}
private:
	std::shared_ptr<Topic> _topic;
};

Appender * makeAppender(std::shared_ptr<Topic> topic){
	return new MyAppender(topic);
}

 }//end namespace stream
